package preprocessor.absorption;

import kb.TBox;
import entities.concept.Concept;
import entities.concept.NotConcept;
import entities.concept.PConcept;

public class EquivalenceAbsorption implements Absorption{

	TBox tbox;
	
	
	public EquivalenceAbsorption(TBox tb){
		this.tbox = tb;
	}
	
	//if returns false, eq axioms are now GCIs
	public boolean absorb(){
		boolean suc = true;
		for(Concept lhs:this.tbox.getTe().keySet()){
			suc &= absorbAxiom(lhs, this.tbox.getTe().get(lhs));
		}	
		return suc;
	}
	
	public boolean absorbAxiom(Concept lhs, Concept rhs){
		boolean abs = false;
		if(lhs == null){
			System.out.println("The given equivalent axiom is empty.");
			return abs;
		}
		// if LHS is already a named concept.
		if(lhs instanceof PConcept){
			//try to absorb into A=>c and -A=>-C
			if(Absorption.NCA){
				try{
					this.tbox.insertTu((PConcept)lhs, rhs);
					this.tbox.insertTnu(new NotConcept(lhs), rhs);
					}catch(Exception e){
						e.printStackTrace();
					}
					abs=true;
			}else{
				//other ops? if negated concept names not allowed, no equivalence absorbption?!Insert to Tg.
				abs = false;
			}
		}else if(lhs.isBinarySimple()){
			// if the LHS is of the form (A and B) =(), no action will be taken at this moment.
		}
		if(!abs){
			//add two GCIs like A->B and B->A
			this.tbox.saveGeneratedGCIs(lhs, rhs);
			this.tbox.saveGeneratedGCIs(rhs, lhs);
		}
		return abs;		
	}
	
}
